﻿using System.IO;
using System.Text;
using UnityEngine;

public static class PathUtil
{
    private static StringBuilder sb = new StringBuilder();
    /// <summary>
    /// 资源包依赖配置文件路径
    /// </summary>
    public const string ABDEPENDS_PATH = "AB_ABDepends.json.uab";
    /// <summary>
    /// 版本配置资源
    /// </summary>
    public const string VS_CONFIG = "AB_VsConfig.json.uab";
    /// <summary>
    /// 版本号
    /// </summary>
    public const string VER_PATH = "Ver.json.uab";
    /// <summary>
    /// 资源包引用脚本包路径
    /// </summary>
    public const string SCRIPT_PATH = "Script.uab";
    /// <summary>
    /// shader包路径
    /// </summary>
    public const string SHADER_PATH = "Shader.uab";
    /// <summary>
    /// json表包路径
    /// </summary>
    public const string CONFIG_PATH = "Config.uab";
    /// <summary>
    /// lua包路径
    /// </summary>
    public const string LUA_PATH = "Lua.uab";
    /// <summary>
    /// dll包路径
    /// </summary>
    public const string DLL_PATH = "Hotcode.dll.bytes.uab";

    public const string PanelUrl = "Prefab/Panel/";
    public const string UITextureUrl = "Prefab/UI/";
    public const string Sound_Path = "Sound/2d/";
    public const string Lua_Path = "Lua/";

    /// <summary>
    /// 加上平台相关streamingAssetsPath前缀路径
    /// </summary>
    /// <param name="path"></param>
    public static string AddStreamingAssetsPath(string path)
    {
        return JoinPath(Application.streamingAssetsPath, path);
    }

    /// <summary>
    /// 加上平台相关persistentDataPath前缀路径
    /// </summary>
    /// <param name="path"></param>
    public static string AddPersistentDataPath(string path)
    {
        return JoinPath(Application.persistentDataPath, path);
    }

    /// <summary>
    /// 拼接路径
    /// </summary>
    /// <param name="pathA"></param>
    /// <param name="pathB"></param>
    public static string JoinPath(string pathA, string pathB)
    {
        sb.Length = 0;
        sb.Append("file:///");
        sb.Append(pathA);
        sb.Append("/");
        sb.Append(pathB);
        return sb.ToString();
    }

    /// <summary>
    /// 检测路径 先到Application.persistentDataPath查看   没有则使用首包的路径
    /// </summary>
    /// <param name="path"></param>
    public static string CheckPath(string path)
    {
        sb.Length = 0;
        sb.Append(Application.persistentDataPath).Append("/").Append(path);
        string newPath = sb.ToString();
        if (!File.Exists(newPath))
        {
            sb.Length = 0;
            sb.Append(Application.streamingAssetsPath).Append("/").Append(path);
            if (Utility.GetPlatForm() == EPlatform.Editor
                || Utility.GetPlatForm() == EPlatform.StandAlone)
            {
                sb.Insert(0, "file:///");
            }
            else if (Utility.GetPlatForm() == EPlatform.IPhone)
            {
                sb.Insert(0, "file://");
            }
        }
        else
        {
            if (Utility.GetPlatForm() == EPlatform.Editor
               || Utility.GetPlatForm() == EPlatform.StandAlone)
            {
                sb.Insert(0, "file:///");
            }
            else 
            {
                sb.Insert(0, "file://");
            }
        }
        newPath = sb.ToString();
        return newPath;
    }

    /// <summary>
    /// 按序拼接路径
    /// </summary>
    /// <param name="arg"></param>
    public static string JoinPath(params string[] arg)
    {
        sb.Length = 0;
        for (int i = 0; i < arg.Length; i++)
        {
            sb.Append(arg[i]);
        }
        return sb.ToString();
    }

    /// <summary>
    /// 文件格式路径转换成ab包文件名称
    /// </summary>
    /// <param name="p"></param>
    public static string FilePathToABName(string path)
    {
        return path.Replace("/", "_").Replace(" ", "-");
    }

    /// <summary>
    /// 解析路径
    /// </summary>
    /// <param name="path"></param>
    public static string ReplaceString(string path)
    {
        string strPath = path.Replace("Assets/", "").Replace("Resources/", "").Replace("Editor/", "");
        return FilePathToABName(strPath);
    }

    /// <summary>
    /// 取得Lua路径
    /// </summary>
    /// <param name="name"></param>
    public static string GetLuaPath(string name)
    {
        string path = Application.dataPath + Lua_Path;
        string lowerName = name.ToLower();
        if (lowerName.EndsWith(".lua"))
        {
            int index = name.LastIndexOf('.');
            name = name.Substring(0, index);
        }
        name = name.Replace('.', '/');
        return path + name + ".lua";
    }
}